﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;
using shfa.snomtastic.data;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eEventLog : EditBase
    {
        #region Public Members

        public AppConfig ac = new AppConfig();

        public Guid SelectedItemId
        {
            get { return (hfSelectedItemId.Value != string.Empty) ? new Guid(hfSelectedItemId.Value) : Guid.Empty; }
            set { hfSelectedItemId.Value = value.ToString(); }
        }

        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {
            BindGridItems();
            
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            rgListItems.ItemCommand += new GridCommandEventHandler(rgListItems_ItemCommand);
            ScriptManager.RegisterClientScriptBlock(this, typeof(EditBase), "ClientEvents", common.DblClickScript, true);
        }

        #endregion

        #region Event Handlers


        protected void rgListItems_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                
                e.Canceled = true;
                Guid id = ((EventLogInfo)e.Item.DataItem).EventLogId;
                RaiseNavEvent(this, new NavEventArgs("eEventLogItem", id));
            }
        }

        #endregion


        public void BindGridItems()
        {
            EventLogController elc = new EventLogController();
            List<EventLogInfo> eliList = elc.SelectEventLogs("EventDate DESC");

            foreach (EventLogInfo eli in eliList)
            {
                eli.EventDate = eli.EventDate.ToLocalTime();
            }

            rgListItems.DataSource = eliList;
            rgListItems.DataBind();


        }


        #region Action Events
        public void ClearEvents()
        {
            EventLogController elc = new EventLogController();
            elc.DeleteEventLogAll();
            RaiseNavEvent(this, new NavEventArgs("eEventLog", new Guid()));
        }

        public void UserDetails(Guid id)
        {
            EventLogController elc = new EventLogController();
            EventLogInfo eli = elc.SelectEventLog(id);
            if (eli != null)
            {
                RaiseNavEvent(this, new NavEventArgs("eUser", eli.UserId));
            }
        }

        public void PhoneDetails(Guid id)
        {
            EventLogController elc = new EventLogController();
            EventLogInfo eli = elc.SelectEventLog(id);
            if (eli != null)
            {
                RaiseNavEvent(this, new NavEventArgs("ePhone", eli.PhoneId));
            }
        }

        #endregion


        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Refresh":
                    RaiseNavEvent(this, new NavEventArgs("eEventLog", new Guid()));
                    break;
                case "Details":
                    if (rgListItems.SelectedValue != null)
                    {
                        RaiseNavEvent(this, new NavEventArgs("eEventLogItem", new Guid(rgListItems.SelectedValue.ToString())));
                    }
                    break;
                case "User":
                    if (rgListItems.SelectedValue != null)
                    {
                        UserDetails(new Guid(rgListItems.SelectedValue.ToString()));
                    }
                    break;
                case "Phone":
                    if (rgListItems.SelectedValue != null)
                    {
                        PhoneDetails(new Guid(rgListItems.SelectedValue.ToString()));
                    }
                    break;
                case "Clear":
                    ClearEvents();
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Refresh", "Refresh Log", "~/images/icons/refresh.png"));
                al.Add(new ActionInfo("Details", "Log Item Details", "~/images/icons/edit.png"));
                al.Add(new ActionInfo("User", "User Details", "~/images/icons/user.png"));
                al.Add(new ActionInfo("Phone", "Phone Details", "~/images/icons/phone.gif"));

                al.Add(new ActionInfo("Clear", "Clear Log", "~/images/icons/delete.png", "Are you sure you wish to clear the event log?", true));


                return al;
            }
        }

        #endregion



    }
}